#pragma once
#include<iostream>
#include<vector>
//#include<functional>
using namespace std;


namespace bite
{
	template<class T>
	class less
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};

	template<class T>
	class greater
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};

	template<class T, class Container = vector<T>, class Compare = less<T>>
	class priority_queue
	{
	public:
		priority_queue()
		{ }

		void adjust_down(int parent, int n)
		{
			int child = parent * 2 + 1;

			while (child < n)
			{
				if (child + 1 < n && cmp(con[child + 1], con[child]))
					++child;

				if (cmp(con[child], con[parent]))
				{
					swap(con[child], con[parent]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					return;
				}
			}
		}

		void adjust_up(int child)
		{
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if (cmp(con[child], con[parent]))
				{
					swap(con[child], con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					return;
				}
			}
		}

		template<class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
			: con(first, last)
		{
			int n = con.size();
			for (int i = (n - 1 - 1) / 2; i >= 0; --i)
				adjust_down(i, n);
		}

		bool empty() const
		{
			return con.empty();
		}

		size_t size() const
		{
			return con.size();
		}

		const T& top() const
		{
			return con[0];
		}

		void push(const T& val)
		{
			con.push_back(val);
			adjust_up(con.size() - 1);
		}

		void pop()
		{
			if (con.empty())
				return;

			swap(con[0], con[con.size() - 1]);
			con.pop_back();
			adjust_down(0, con.size());
		}

	private:
		Container con;
		Compare cmp;
	};
}